<?php
include "../lib/DAL/TblKhoahoc.php";

class TblKhoahocBLL{
    
    function __construct(){
    }
    
    function __destruct(){
    }
     function setKhoahoc($row){
        $k_hoc = new TblKhoahoc();
        $k_hoc->setKhoahoc($row['id_khoahoc'],$row['nam_khoahoc'],$row['mota_khoahoc'],$row['id_dv_ctdt'],$row['ma_cb']);
        return $k_hoc;
    }
    function setKhoahocFull($row){
        $k_hoc = new TblKhoahoc();
        $k_hoc->setKhoahocFull($row['id_khoahoc'],$row['nam_khoahoc'],$row['mota_khoahoc'],$row['id_dv_ctdt'],$row['ma_cb'],$row['ten_donvi'],$row['ma_ctdt'],$row['hoten_cb']);
        return $k_hoc;
    }
    
    //d
    public function getKhoahocinfo($makhoa){
        $k_hoc = new TblKhoahoc();
        $sql = "select * from tbl_khoahoc where id_khoahoc =".$makhoa."";       
        $row = mysql_fetch_array(mysql_query($sql));
       return $this->setKhoahoc($row);        
    }
    public function getnam($value){   
        $sql = "select * from tbl_khoahoc where nam_khoahoc =".$value.""; 
        $result = mysql_query($sql); 
        $arr_khoahoc = array();
        $i=0;  
        while($row = mysql_fetch_array($result)){   
                $arr_khoahoc[$i] =  $this->setKhoahoc($row);
                $i++;
              }             
        return $arr_khoahoc;   
        
    }
    public function getListkhoahoc(){
      $result = mysql_query("select * from tbl_khoahoc");
      $arr_khoahoc = array();
      $i=0;  
      while($row = mysql_fetch_array($result)){   
            $arr_khoahoc[$i] =  $this->setKhoahoc($row);
            $i++;
          } 
      return $arr_khoahoc;
    }
     public function getListkhoahocFull(){
      $result = mysql_query("select id_khoahoc,nam_khoahoc,mota_khoahoc,tbl_khoahoc.id_dv_ctdt,ten_donvi,ma_ctdt,tbl_khoahoc.ma_cb,hoten_cb 
                                from tbl_khoahoc join tbl_canbo  
                                on tbl_khoahoc.ma_cb = tbl_canbo.ma_cb 
                                join tbl_donvi_ctdt on tbl_khoahoc.id_dv_ctdt = tbl_donvi_ctdt.id_dv_ctdt
                                join tbl_donvi on tbl_donvi_ctdt.ma_donvi = tbl_donvi.ma_donvi");
      $arr_khoahoc = array();
      $i=0;  
      while($row = mysql_fetch_array($result)){   
            $arr_khoahoc[$i] =  $this->setKhoahocFull($row);
            $i++;
          } 
      return $arr_khoahoc;
    }
    
    public function getlistcb(){
      $result = mysql_query("select ma_cb,hoten_cb from tbl_canbo");
      $arr_canbo = array();
      $i=0;  
      while($row = mysql_fetch_array($result)){   
            $arr_canbo[$i]['id'] =  $row['ma_cb'];
            $arr_canbo[$i]['hoten'] =  $row['hoten_cb'];   
            $i++;
          }   
      //     print_r($arr_canbo);
      return $arr_canbo;  
    }
    public function getlistdv(){
      $result = mysql_query("select id_dv_ctdt,ten_donvi,ma_ctdt from tbl_donvi_ctdt join tbl_donvi on tbl_donvi_ctdt.ma_donvi = tbl_donvi.ma_donvi");
      $arr_donvi = array();
      $i=0;  
      while($row = mysql_fetch_array($result)){   
            $arr_donvi[$i]['id_dv'] =  $row['id_dv_ctdt'];
            $arr_donvi[$i]['tenctdt'] =  $row['ma_ctdt'].' thuộc '.$row['ten_donvi'];   
            $i++;
          } 
      return $arr_donvi;  
    }
    
    public function insertKhoahoc($khoahoc){
      $sql = "insert into tbl_khoahoc(nam_khoahoc,mota_khoahoc,id_dv_ctdt,ma_cb)
            values('".$khoahoc->getnam_khoahoc()."','".$khoahoc->getmota_khoahoc()."','".$khoahoc->getid_dv_ctdt()."','".$khoahoc->getma_cb()."')";  
      $result = mysql_query($sql) or die (mysql_error());
      return $result;
    }
    
    public function updateKhoahoc($khoahoc){
        $sql ="UPDATE tbl_khoahoc SET           
            nam_khoahoc = '".$khoahoc->getnam_khoahoc()."' 
            ,mota_khoahoc = '".$khoahoc->getmota_khoahoc()."' 
            ,id_dv_ctdt = '".$khoahoc->getid_dv_ctdt()."'
            ,ma_cb = '".$khoahoc->getma_cb()."' WHERE id_khoahoc = ".$khoahoc->getid_khoahoc();           
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    
    public function deletekhoahoc($ma_khoahoc){
        $sql="delete from tbl_khoahoc where id_khoahoc = '".$ma_khoahoc."';";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
        
    }
    public function getKhoahocinfoSerch($namkhoahoc,$canbo){ 
        $arr_khoahoc = array();
        $i=0;  
        $sql = "select id_khoahoc,nam_khoahoc,mota_khoahoc,tbl_khoahoc.id_dv_ctdt,ten_donvi,ma_ctdt,tbl_khoahoc.ma_cb,hoten_cb 
                                from tbl_khoahoc join tbl_canbo  
                                on tbl_khoahoc.ma_cb = tbl_canbo.ma_cb 
                                join tbl_donvi_ctdt on tbl_khoahoc.id_dv_ctdt = tbl_donvi_ctdt.id_dv_ctdt
                                join tbl_donvi on tbl_donvi_ctdt.ma_donvi = tbl_donvi.ma_donvi ";
                                
       if($namkhoahoc!=''&&$canbo=='')
        {  
               $sql.= " where nam_khoahoc =".$namkhoahoc.""; 
               $dem=1;
        }
        if($namkhoahoc!=''&&$canbo!='')
        {       
               $sql.= "where nam_khoahoc =".$namkhoahoc." and  hoten_cb LIKE '%".$canbo."%' ";    
        }
        if($namkhoahoc==''&&$canbo!='')
        {       
               $sql.= "where hoten_cb LIKE ='%".$canbo."%' ";    
        }   
        $result = mysql_query($sql);    
        while($row = mysql_fetch_array($result)){   
                $arr_khoahoc[$i] =  $this->setKhoahocFull($row);
                $i++;
              } 
        return $arr_khoahoc;      
    }
}

?>